<?php
namespace app\admin\controller;

use think\Controller;
use think\Cookie;
use think\Exception;
use think\Log;
use think\Db;

class Goods extends  Base
{
    /**
     * 商品列表
     * @author wcy
     * @version 2017年4月28日11:43:43
     */
    public function goodsList(){
        try{
            if($this->request->isPost()||$this->request->isAjax()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=trim($_POST["search"]);
                }
                $data=model("Goods")->goodsList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("goodslist");
            }
        }catch (Exception $e){
            Log::check($e->getMessage());
            return view("goodslist");
        }
    }
    /**
     * 添加商品
     * @author wcy
     * @version 2017年4月28日13:46:24
     */
    public function addGoods(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data["goods_name"]=$_POST["goods_name"];
                $data["img"]=$this->upload("goods/upload");
                $count=model("Goods")->getGoodsName($data['goods_name']);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"商品名称已经存在!"]);
                }
                $data["number"]=$_POST["number"];
                $data["status"]=$_POST["status"];
                $data["price"]=$_POST["price"];
                $data["content"]=$_POST["editorValue"];
                $goods_imgs=$_POST["goods_imgs"];
                $res=model("Goods")->addGoods($data,$goods_imgs);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功!"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败!"]);
                }
            }else{
                return view("addgoods");
            }
        }catch (Exception $e){
            Log::check($e->getMessage());
            return view("addgoods");
        }
    }
    /**
     * 编辑商品
     * @author wcy
     * @version 2017年5月2日13:40:17
     */
    public function editGoods(){
        try{
            if($this->request->isPost()||$this->request->isAjax()){
                $id=$_POST["id"];
                $data["goods_name"]=$_POST["goods_name"];
                $count=model("Goods")->getGoodsNameEdit($id,$data['goods_name']);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"商品名称已经存在!"]);
                }
                $data["number"]=$_POST["number"];
                $data["status"]=$_POST["status"];
                $data["price"]=$_POST["price"];
                if(!empty($_POST["editorValue"])) {
                    $data["content"]=$_POST["editorValue"];
                }
                $img=$this->upload("goods/upload");
                if(!empty($img)){
                    $data["img"]=$img;
                }
                $goods_imgs=$_POST["goods_imgs"];
                $res=model("Goods")->editGoods($id,$data,$goods_imgs);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $goodsImgData=model("GoodsImg")->getGoodsImg($id);
                $data=model("Goods")->getGoodsById($id);
                $this->assign("goods_data",$data);
                $this->assign("goodsImgData",$goodsImgData);
                return view("editgoods");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editgoods");
        }
    }
    /**
     *修改状态
     * @author wcy
     * @version 2017年5月2日14:29:56
     */
    public function editStatus(){
        try{
            $id=$_POST["id"];
            $data["status"]=$_POST["status"];
            $res=model("Goods")->editGoodsStatus($id,$data);
            if($res!==false){
                return json(["code"=>1,"msg"=>"修改成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"修改失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
        }
    }
    /**
     * 删除商品
     * @author wcy
     * @version 2017年5月2日10:08:01
     */
    public function delGoods(){
        try{
            $id=$_POST["id"];
            $res=model("Goods")->delGoods($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
        }
    }
    /**
     * 删除图片
     * @author wcy
     * @version 2017年5月5日09:47:58
     */
    public function delGoodsImg(){
        try{
            $id=$_POST["id"];
            $res=model("GoodsImg")->delGoodsImg($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统出错，请刷新页面"]);
        }
    }

}